package org.mobilesynergies.epic.client.parameter;

import java.util.HashMap;
import java.util.Set;

import org.jivesoftware.smackx.FormField;

public abstract class Parameter {

	public static final String SPECIALIZECHARACTER = "_";
	
	public static final String TYPENAME_FLOAT = "float";
	public static final String TYPENAME_INT = "int";
	public static final String TYPENAME_STRING = "string";
	public static final String TYPENAME_BOOLEAN = "boolean";
	
	public static final String KEY_TYPE = "type";
	public static final String KEY_NAME = "name";
	public static final String KEY_VALUE = "value";
	public static final String KEY_MIN = "min";
	public static final String KEY_MAX = "max";
	public static final String KEY_STEPSIZE = "step";
	

	public abstract String getType();
	
	public abstract String getValueAsString();
	
	public abstract Set<FormField> toFormFields(String label);

	public abstract String toXml(String name);
	
	protected String toXml(String name, String type, String value, String extraattributes,
			String extraxml) {
		String result ="<parameter name=\""+name+"\" type=\""+type+"\"";
		if((extraattributes!=null)&&(extraattributes.length()>0)) {
			result+=" "+extraattributes; 
		}
		
		if(((extraxml==null)||(extraxml.length()==0)) && ((value==null)||(value.length()==0))) {
			//nothing else to add
			result+= "/>";
		} else {
			result+= ">";
			if((value!=null)&&(value.length()>0)){
				result+= value; 
			}
			if((extraxml!=null)&&(extraxml.length()>0)){ 
				result+= extraxml;
			}
			result+= "</parameter>";
		}
		return result;
	}

	public static Parameter createParameter(HashMap<String, String> map) throws ParameterException {
		
		String type = map.get(KEY_TYPE);
		if(! map.containsKey(KEY_TYPE)) {
			//Type String is assumed 
			type = TYPENAME_STRING;
		}
				
		Parameter p = null;
		if(type.equalsIgnoreCase(TYPENAME_BOOLEAN)){
			BooleanParameter param = new BooleanParameter(map);
			p = param;
		}else if(type.equalsIgnoreCase(TYPENAME_STRING)){
			StringParameter param = new StringParameter(map);
			p = param;
		}else if(type.equalsIgnoreCase(TYPENAME_INT)){
			IntParameter param = new IntParameter(map);
			p = param;
		}else if(type.equalsIgnoreCase(TYPENAME_FLOAT)){
			FloatParameter param = new FloatParameter(map);
			p = param;
		}else  {
			throw new ParameterException("Unknown parameter type.");
		}
			
		return p;
	}

	
	
}
